package swing;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

public class SwingPopupMenu extends JPopupMenu {
	private JPopupMenu popup = null;

	public SwingPopupMenu(JPanel p) {
		popup = new JPopupMenu();
		JMenu m = new JMenu("new1");
		popup.add(m);
		JMenuItem m1 = new JMenuItem("new");
		m.add(m1);
		JMenuItem m2 = new JMenuItem("new2");
		m.add(m2);
		JMenuItem m3 = new JMenuItem("new3");
		m.add(m3);
		JMenu m11 = new JMenu("new2");
		popup.add(m11);
		JMenuItem c1 = new JMenuItem("new");
		m11.add(c1);
		p.addMouseListener(new PopupMenuListener());
		
	}
		// SwingManin의 contentPane(JPanel)에
		// PopupMenuListener를
		// MouseListener로 등록
	
	
	class PopupMenuListener extends MouseAdapter{
		
		public void mousePressed(MouseEvent e) {
			if (e.isPopupTrigger()) {
				showMenu(e);
			}
		}
		public void mouseReleased(MouseEvent e) {
			if (e.isPopupTrigger()) {
				showMenu(e);
			}
		}
		private void showMenu(MouseEvent e) {
			popup.show(e.getComponent(), e.getX(), e.getY());
		}
	}


}
		